/*
 * 创建日期 2009-11-11
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.cpp.core.item;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import  org.fantasy.cpp.core.bean.ParamContext;
import  org.fantasy.cpp.core.bean.ParamContextImpl;
import  org.fantasy.cpp.core.html.QueryHtml;
import  org.fantasy.cpp.core.pojo.ItemAttr;
import  org.fantasy.cpp.core.pojo.QueryItem;
import  org.fantasy.common.util.StringUtil;

/**
 *  功能说明
 * @author: 王文成
 * @version: 1.0
 * @since 2009-11-11
 */
public abstract class AbstractItem implements ItemSupport {
	protected HttpServletRequest request;

	protected QueryItem item;

	/**
	 * 取得查询条件HTML
	 * 
	 * @return
	 * @throws Exception
	 */
	public abstract String getHtml() throws Exception;

	public void setItem(QueryItem item) {
		this.item = item;
	}

	public void setHttpServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@SuppressWarnings("all")
	protected ParamContext getParamContext() {
		ParamContext param = new ParamContextImpl();
		param.set(QueryHtml.ITEM_ID, item.getItemId().toString());
		param.set(QueryHtml.CODE, item.getItemCode());
		param.set(QueryHtml.NAME, item.getItemName());
		param.set(QueryHtml.FORMAT, item.getItemFormat());
		param.set(QueryHtml.CHECKED, String.valueOf(item.getItemCheck()));
		param.set(QueryHtml.DEF_VALUE, item.getDefValue());
		param.set(QueryHtml.CLASS_NAME, item.getClassName());
		param.set(QueryHtml.CSS_STYLE, item.getCssStyle());
		param.set(QueryHtml.CSS_CLASS, item.getCssClass());
		param.set(QueryHtml.AUTO_QUERY, String.valueOf(item.getAutoQuery()));
		List<ItemAttr> attrs = item.getAttrs();
		// 扩展属性
		for (ItemAttr attr : attrs) {
			param.set(attr.getAttrCode(), attr.getAttrValue());
		}
		return param;
	}

	/**
	 * 取得样式
	 * 
	 * @return
	 */
	protected String getStyleHtml() {
		StringBuffer html = new StringBuffer(32);
		String style = item.getCssStyle();
		if (StringUtil.isValid(style)) {
			html.append(" style='" + style + "'");
		}
		return html.toString();
	}

	/**
	 * 取得样式
	 * 
	 * @return
	 */
	protected String getClassHtml() {
		StringBuffer html = new StringBuffer(32);
		String className = item.getCssClass();
		if (StringUtil.isValid(className)) {
			html.append(" class='" + className + "'");
		}
		return html.toString();
	}
}
